import _ from 'lodash'
import { removeHtmlTag } from '@/formgen/src/utils'

export function formatTableColumn(item) {
  // 如果是个json对象 格式化成json字符串
  if (['SELECT', 'CASCADER', 'IMAGE_SELECT'].includes(item.type)) {
    return {
      field: item.value,
      title: item.label,
      minWidth: '150',
      slots: {
        // 使用 JSX 渲染
        default: ({ row }) => {
          if (_.isArray(row[item.value])) {
            return row[item.value].join(',')
          } else {
            return row[item.value]
          }
        }
      }
    }
  }
  if ('PROVINCE_CITY' === item.type) {
    return {
      field: item.value,
      title: item.label,
      minWidth: '150',
      slots: {
        // 使用 JSX 渲染
        default: ({ row }) => {
          if (_.isArray(row[item.value])) {
            return row[item.value].join(',')
          } else {
            return (
              (_.get(row[item.value], 'cascadeValue') ? _.get(row[item.value], 'cascadeValue') : '') +
              ' ' +
              (_.get(row[item.value], 'detailAddr') ? _.get(row[item.value], 'detailAddr') : '')
            )
          }
        }
      }
    }
  } else if ('IMAGE_UPLOAD' === item.type) {
    return {
      field: item.value,
      title: item.label,
      minWidth: '150',
      slots: { default: 'img_default' }
    }
  } else if ('UPLOAD' === item.type) {
    return {
      field: item.value,
      title: item.label,
      minWidth: '150',
      slots: { default: 'file_default' }
    }
  } else if ('SIGN_PAD' === item.type) {
    return {
      field: item.value,
      title: item.label,
      minWidth: '150',
      slots: { default: 'signpad_default' }
    }
  } else if ('MATRIX_INPUT' === item.type) {
    return {
      field: item.value,
      title: item.label,
      minWidth: '150',
      formatter: (event) => formatMatrixInputData(event.row[item.value], item)
    }
  }else if ('MATRIX_TABLE' === item.type) {
    return {
      field: item.value,
      title: item.label,
      minWidth: '150',
      
      // slots: {
      //   // 使用 JSX 渲染
      //   default: ({ row }) => {
      //     return formatMatrixTableData(row[item.value], item)
      //   }
      // }
      slots: { default: 'matrix_table_default' },
      // formatter: (event) => formatMatrixTableData(event.row[item.value], item)
    }
  } else if (['MATRIX_SELECT', 'MATRIX_SCALE'].includes(item.type)) {
    return {
      field: item.value,
      title: item.label,
      minWidth: '150',
      formatter: (event) => formatMatrixSelectData(event.row[item.value], item)
    }
  } else if ('SUB_FORM' === item.type) {
    return {
      field: item.value,
      title: item.label,
      minWidth: '150',
      formatter: (event) => formatSubFormData(event.row[item.value], item)
    }
  } else if (
    'RESERVE_DAY' === item.type ||
    'RESERVE_TIME_RANGE' === item.type
  ) {
    return {
      field: item.value,
      title: item.label,
      minWidth: '150',
      formatter: (event) => formatReserveDayData(event.row[item.value], item)
    }
  } else if ('GOODS_SELECT' === item.type) {
    return {
      field: item.value,
      title: item.label,
      minWidth: '150',
      formatter: (event) => formatGoodsData(event.row[item.value], item)
    }
  } else if ('SORT' === item.type) {
    return {
      field: item.value,
      title: item.label,
      minWidth: '150',
      slots: {
        default: ({ row }) => {
          if (row[item.value]) {
            let v = row[item.value].map((item) => item.label).join(',')
            return [v]
          }
        }
      }
    }
  } else if (['USER_SELECT', 'DEPT_SELECT'].includes(item.type)) {
    return {
      field: item.value,
      title: item.label,
      minWidth: '150',
      slots: {
        default: ({ row }) => {
          if (row[item.value]) {
            let v = row[item.value].map((item) => item.name).join(',')
            return [v]
          }
        }
      }
    }
  }else if ('HORIZONTAL_INPUT' === item.type) {
    return {
      field: item.value,
      title: item.label,
      minWidth: '150',
      slots: {
        default: ({ row }) => {
          console.log(row[item.value],'44444444')
          if (Array.isArray(row[item.value]) && row[item.value].length) {
            let v = row[item.value].map((item) => item).join(';')
            return [v]
          }else{
            return [row[item.value]]
          }
        }
      }
    }
  }
  return { field: item.value, minWidth: '200px', title: item.label }
}

/**
 * 格式化预约时间
 * @param value
 * @param item
 * @returns {string|null}
 */
export function formatReserveDayData(value, item) {
  if (!value) {
    return null
  }
  let text = ''
  Object.keys(value).forEach((key) => {
    let project = item.scheme.reserveProjectList.find((row) => row.id == key)
    if (project) {
      text += `${project.name}：${value[key]} `
    }
  })
  return text
}

/**
 * 格式化商品购买
 * @param value
 * @param item
 * @returns {string|null}
 */
export function formatGoodsData(value, item) {
  if (!value) {
    return null
  }
  let text = ''
  Object.keys(value).forEach((key) => {
    let project = item.scheme.goodsList.find((row) => row.id == key)
    if (project) {
      text += `${project.goodsName}：${value[key]}个 `
    }
  })
  return text
}

/**
 * 格式化矩阵输入
 * @param value
 * @param item
 * @returns {string|null}
 */
export function formatMatrixInputData(value, item) {
  if (!value) {
    return null
  }
  let text = ''
  Object.keys(value).forEach((key) => {
    let row = item.scheme.table.rows.find((row) => row.id == key)
    if (row) {
      let rowText = ''
      let rowValue = value[key]
      Object.keys(rowValue).forEach((ckey) => {
        let col = item.scheme.table.columns.find((col) => col.id == ckey)
        if (col) {
          rowText += `${col.label}：${rowValue[ckey]}，`
        }
      })
      text += ` ${row.label}：( ${rowText} )`
    }
  })
  return text
}
export function formatMatrixTableData(value, item) {
  if (!value) {
    return null
  }
  let data ={}
  Object.keys(value).forEach((key) => {
    let row = item.scheme.table.rows.find((row) => row.id == key)
    if (row) {
      let rowValue = value[key]
      let colObj = {}
      Object.keys(rowValue).forEach((ckey) => {
        let col = item.scheme.table.columns.find((col) => col.id == ckey)
        if (col) {//多选，单选，上传等
            // 多选：1
            colObj[col.label]=rowValue[ckey]
        }
      })
      data[row.label]=colObj
    }
  })
  return [data]
}
/**
 * 格式化矩阵选择
 * @param value
 * @param item
 * @returns {string|null}
 */
export function formatMatrixSelectData(value, item) {
  if (!value) {
    return null
  }
  let text = ''
  Object.keys(value).forEach((key) => {
    let row = item.scheme.table.rows.find((row) => row.id == key)
    if (row) {
      text += ` ${row.label}：( ${value[key]} )`
    }
  })
  return text
}

/**
 * 格式化矩阵选择
 * @param value
 * @param item
 * @returns {string|null}
 */
export function formatSubFormData(value, item) {
  if (!value) {
    return null
  }
  let keyLabelObj = {}
  let childList = item.scheme.childList
  childList.forEach((c) => {
    keyLabelObj[c.vModel] = removeHtmlTag(c.config.label)
  })
  let subFormValue = value.map((subValue) => {
    let tempObj = {}
    Object.keys(subValue).forEach((sKey) => {
      tempObj[keyLabelObj[sKey]] = subValue[sKey]
    })
    return tempObj
  })
  return JSON.stringify(subFormValue)
}
